package connection;

import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.Socket;

public class Reader extends Thread {

	private int letter;
	public Socket fics;
	private BufferedReader br;
	private static BufferedWriter bw;
	public TextArea con = new TextArea();

	static PipedInputStream is;
	static PipedOutputStream os;

	static boolean reading = false;

	private char pom;

	Font f = new Font(Font.MONOSPACED, Font.PLAIN, 10);

	public Reader(BufferedReader br, BufferedWriter bw) {
		// this.fics = fics;
		this.br = br;
		Reader.bw = bw;
		con.setFont(f);
		con.setEditable(false);
		con.setBackground(Color.white);
		os = new PipedOutputStream();

		try {
			is = new PipedInputStream(os);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TextArea textPan() {
		return con;
	}

	public static void setCtu() {
		reading = true;
	}

	public static PipedInputStream getReader() {
		return is;
	}

	public static BufferedWriter getWriter() {
		return bw;
	}

	public void run() {
		while (true) {
			try {
				letter = br.read();
				if (reading) {
					os.write(letter);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			pom = (char) letter;
			con.append(String.valueOf(pom));
		}
	}

}
